package dao

import (
	"RedWork/util"
	"encoding/json"
	//"bufio"
	//"encoding/json"
	"fmt"
	"github.com/go-sql-driver/mysql"
	//jsonma "github.com/alecthomas/jsonschema"
	//"image/color"
)

func QueryAllGoods(page_g int) (data []GoodInfo) {
	fmt.Println("page_v =", page_g)
	goods := make([]GoodInfo, 0)
	//var goods []good_info
	var sqlStr string
	if page_g != -1 {
		sqlStr = fmt.Sprintf("select good_id, good_name, price, store_id, store_name, category, good_image, introduce from good_info limit %d,10", page_g)
	} else {
		sqlStr = "select good_id, good_name, price, store_id, store_name, category, good_image, introduce from good_info"
	}

	rows, err := DB.Query(sqlStr)
	if ok, errR, _ := util.ErrHandle(err); ok {
		fmt.Println(errR.(*mysql.MySQLError).Message)
	} else {
		fmt.Println(err)
	}
	fmt.Println("rows=", rows)
	defer rows.Close()
	for rows.Next() {
		var good_data GoodInfo
		err := rows.Scan(
			&good_data.GoodID,
			&good_data.GoodName,
			&good_data.Price,
			&good_data.StoreID,
			&good_data.StoreName,
			&good_data.Category,
			&good_data.Good_image,
			&good_data.Introduce)
		fmt.Println(good_data)
		if err != nil {
			if ok, err, _ := util.ErrHandle(err); ok {
				fmt.Println(err)
			}
			fmt.Println(err)
			break
		}
		goods = append(goods, good_data)
		//fmt.Println(goods)
	}
	if len(goods) == 0 {
		return nil
	}
	fmt.Println(goods[0])
	fmt.Println(goods[1])
	bytes, _ := json.Marshal(goods)
	fmt.Println(string(bytes))
	return goods
}

func QueryAKindGood(goodname string, sort int) (data []GoodInfo) {
	fmt.Println("goodname = ", goodname)
	sqlStr := fmt.Sprintf("select good_id, good_name, price, store_id, store_name, category, good_image,introduce from good_info where good_name like '%%%s%%'", goodname)
	if sort == 1 {
		sqlStr = sqlStr + " order by turnover desc"
	} else if sort == 4 {
		sqlStr = sqlStr + " order by price asc"
	} else if sort == 5 {
		sqlStr = sqlStr + " order by price desc"
	}
	goods := make([]GoodInfo, 0)
	fmt.Println(sqlStr)
	rows, err := DB.Query(sqlStr)
	defer rows.Close()
	if err != nil {
		if ok, _, _ := util.ErrHandle(err); ok {
			fmt.Println("mysql err")
		}
	}
	fmt.Println("rows=", rows)
	if rows != nil {
		fmt.Println("Enter?")
		for rows.Next() {
			var good_data GoodInfo
			err := rows.Scan(
				&good_data.GoodID,
				&good_data.GoodName,
				&good_data.Price,
				&good_data.StoreID,
				&good_data.StoreName,
				&good_data.Category,
				&good_data.Good_image,
				&good_data.Introduce)
			fmt.Println(good_data)
			if err != nil {
				if ok, _, _ := util.ErrHandle(err); ok {
					fmt.Println("mysql err")
				}
				break
			}
			goods = append(goods, good_data)
			//fmt.Println(goods)
		}
	}

	if len(goods) == 0 {
		return nil
	}
	bytes, _ := json.Marshal(goods)
	fmt.Println(string(bytes))
	return goods
}

func QueryCategoryGoods(code int) (data []GoodInfo) {
	goods := make([]GoodInfo, 0)
	fmt.Println("QueryCategoryGood = ", code)
	sqlStr := fmt.Sprintf("select good_id, good_name, price, store_id, store_name, category, good_image,introduce from good_info where category = %d", code)
	fmt.Println(sqlStr)
	rows, err := DB.Query(sqlStr)
	defer rows.Close()
	if err != nil {
		if ok, _, _ := util.ErrHandle(err); ok {
			fmt.Println("mysql err")
		}
	}
	fmt.Println("rows=", rows)
	if rows != nil {
		fmt.Println("Enter?")
		for rows.Next() {
			var good_data GoodInfo
			err := rows.Scan(
				&good_data.GoodID,
				&good_data.GoodName,
				&good_data.Price,
				&good_data.StoreID,
				&good_data.StoreName,
				&good_data.Category,
				&good_data.Good_image,
				&good_data.Introduce)
			if err != nil {
				if ok, _, _ := util.ErrHandle(err); ok {
					fmt.Println("mysql err")
				}
				break
			}
			goods = append(goods, good_data)
		}
	}

	if len(goods) == 0 {
		return nil
	}
	bytes, _ := json.Marshal(goods)
	fmt.Println(string(bytes))
	return goods
}
